#!/bin/sh
# kind of like emacs' regex-align command
# How it work: align '::' file.txt

cat $2 | awk -F$1 '{
  if(max < length($1)) {
    max = length($1)
  }
  list[NR] = $0
}
END {
  for(i in list) {
    if(list[i] ~ FS) {
      split(list[i], a, FS);
      diff = max - length(a[1])
      if(diff == 0) {
        print a[1] FS a[2]
      } else if(diff > 0) {
        spaces = ""
        for(; diff > 0 ; diff--)
          spaces = spaces " "
        print a[1] spaces FS a[2]
      }
    } else {
      print list[i]
    }
  }
}'
